#include "GlobalFunctions.h"

void drawSmallSquares(QPainter &painter, int x, int y)
{
	for (int i = x; i < (x + 48); ++i)
	{
		for (int j = y + (i % 2); j < (y + 48); j += 2)
			painter.drawPoint(i, j);
	}
}

void drawLargeSquares(QPainter &painter, int x, int y)
{
	for (int i = 0; i < 48; i += 2)
	{
		for (int j = i % 4; j < 48; j += 4)
		{
			painter.drawPoint(x + i + 0, y + j + 0);
			painter.drawPoint(x + i + 1, y + j + 0);
			painter.drawPoint(x + i + 0, y + j + 1);
			painter.drawPoint(x + i + 1, y + j + 1);
		}
	}
}

void drawThinHorizontalLines(QPainter &painter, int x, int y)
{
	for (int i = y; i < (y + 48); i += 2)
		painter.drawLine(x, i, x + 47, i);
}

void drawThickHorizontalLines(QPainter &painter, int x, int y)
{
	painter.drawLine(x, y, x + 47, y);

	for (int i = 3; i < 47; i += 4)
	{
		painter.drawLine(x, y + i, x + 47, y + i);
		painter.drawLine(x, y + i + 1, x + 47, y + i + 1);
	}
	painter.drawLine(x, y + 47, x + 47, y + 47);
}

void drawThinVerticalLines(QPainter &painter, int x, int y)
{
	for (int i = x; i < (x + 48); i += 2)
		painter.drawLine(i, y, i, y + 47);
}

void drawThickVerticalLines(QPainter &painter, int x, int y)
{
	painter.drawLine(x, y, x, y + 47);

	for (int i = 1; i < 47; i += 4)
	{
		painter.drawLine(x + i + 0, y, x + i + 0, y + 47);
		painter.drawLine(x + i + 1, y, x + i + 1, y + 47);
	}
}

void drawVerticalGradient(QPainter &painter, int height, int x0, QColor color0, int x1, QColor color1)
{
	int r0 = color0.red();
	int g0 = color0.green();
	int b0 = color0.blue();

	int r1 = color1.red();
	int g1 = color1.green();
	int b1 = color1.blue();

	int dx = x1 - x0;

	for (int x = x0; x < x1; ++x)
	{
		painter.setPen(QColor::fromRgb(((x - x0)*r1 + (x1 - x)*r0) / dx,
			((x - x0)*g1 + (x1 - x)*g0) / dx, ((x - x0)*b1 + (x1 - x)*b0) / dx));
		painter.drawLine(x, 0, x, height);
	}
}

const int color_factors[] = { 239,243,247,251,255 };

void drawGradientSquares(QPainter &painter, int x0, int y0, int size, QColor base_color)
{
	int factor = color_factors[sizeof(color_factors) / sizeof(color_factors[0]) - 1];
	painter.fillRect(x0, y0, size, size, QColor::fromRgb(base_color.red()*factor,
		base_color.green()*factor, base_color.blue()*factor));

	factor = color_factors[0];
	painter.fillRect(x0 + size, y0, size, size, QColor::fromRgb(base_color.red()*factor,
		base_color.green()*factor, base_color.blue()*factor));

	int step = size / (2 * sizeof(color_factors) / sizeof(color_factors[0]) + 1);

	int left = x0;
	int top = y0;
	int small_size = size;

	for (int i = 0; i < int(sizeof(color_factors) / sizeof(color_factors[0])); ++i)
	{
		left += step;
		top += step;
		small_size -= (2 * step);

		painter.fillRect(left, top, small_size, small_size, QColor::fromRgb(base_color.red()*color_factors[i],
			base_color.green()*color_factors[i], base_color.blue()*color_factors[i]));
	}

	left = x0 + size;
	top = y0;
	small_size = size;

	for (int i = int(sizeof(color_factors) / sizeof(color_factors[0])) - 1; i >= 0; --i)
	{
		left += step;
		top += step;
		small_size -= (2 * step);

		painter.fillRect(left, top, small_size, small_size, QColor::fromRgb(base_color.red()*color_factors[i],
			base_color.green()*color_factors[i], base_color.blue()*color_factors[i]));
	}
}
